clc;
clear all;
close all;

[num,txt,raw]=xlsread('match.xlsx');
[num1,txt1,raw1]=xlsread('discriminant.xlsx');
raw2=raw1(3:end,:);
a1=raw2(:,3);
a2=raw2(:,2);
a3=raw2(:,8:15);
a4=raw2(:,17:107);
a5=raw2(:,115:128);
a6=[a3,a4,a5];
raw3=raw(2:end,:);
raw4=raw3(:,1);
raw5=raw3(:,3);
raw6=raw3(:,7);
raw7=raw3(:,8);
L=raw3(:,6);
[mm,nn]=size(L);
L1=[];
for i=1:mm
    aa=2;
    L1=[L1;{aa}];
end
name1=[raw4;raw7];
time1=[raw5;raw6];
label1=[L;L1];
[mm1,nn1]=size(name1);
[mm2,nn2]=size(a2);
a11=cell2mat(a1);
time2=cell2mat(time1);
qc=[];
qc1=[];
for i=1:mm1
    name2=cell2mat(name1(i));
    name2=string(name2);
    time3=time2(i);
    for j=1:mm2
        name3=cell2mat(a2(j));
        name3=string(name3);
        if (strcmp(name2,name3)==1)&&time3==a11(j)
           qc=[qc;j];
           qc1=[qc1;i];
         break;
        end
    end
end
newname=name1(qc1);
newtime=time2(qc1);
newlabel=label1(qc1);
a7=a6(qc,:);
[mm3,nn3]=size(newtime);
newtime1=[];
for i=1:mm3
    newtime1=[newtime1;{newtime(i)}];
end
data1=[newname,newtime1,a7,newlabel];
[mm4,nn4]=size(data1);
ty=[];
for j=3:nn4
        [mm5,nn6]=find(isnan(cell2mat(data1(:,j))));
        if isempty(nn6)==0
            ty=[ty,j];
        end
end
data1(:,ty)=[];
data=cell2mat(data1(:,3:end-1));
label=cell2mat(data1(:,end));
[mm6,nn6]=size(data);
df=[];
for i=1:nn6
[k1,k2]=find(data(:,i)==0);
if mm6==sum(k2)
    df=[df,i];
end
end

df1=[];
for i=1:nn6
[k1,k2]=find(data(:,i)==1);
if mm6==sum(k2)
    df1=[df1,i];
end
end
df2=[df,df1];
data(:,df)=[];

data=zscore(data);     
r=corrcoef(data);      
[vec1,lamda,rate]=pcacov(r);               
f=repmat(sign(sum(vec1)),size(vec1,1),1);    
vec2=vec1.*f;           
num = max(find(lamda>1)); 
df=data*vec2(:,1:num);   
tf=df*rate(1:num)/100;    
[stf,ind]=sort(tf,'descend'); 
stf=stf'; 
ind=ind';       
newdata=data(ind,:);
newdata11=newdata;
ccoefficients = corr(newdata11, cell2mat(newlabel), 'Type', 'Pearson');
threshold = 0.01;
hfeatures = find(abs(ccoefficients) > threshold);
disp(hfeatures );
newdata=newdata11(:,hfeatures);
total_N = size(newdata,1);     
trainSet_N = round(0.8*total_N);   
validationSet_N = round(0.2*total_N); 
id = randperm(total_N);
trainSet_id = id(:,1:trainSet_N); 
trainSet = newdata(trainSet_id,:);
train_index=label(trainSet_id,:);
validationSet_id = id(:,(trainSet_N+1):(trainSet_N+validationSet_N)); 
validationSet = newdata(validationSet_id,:);
validationSet_index=label(validationSet_id,:);
[p_train, ps_input] = mapminmax(trainSet', 0, 1);
p_test  = mapminmax('apply', validationSet', ps_input);

train_x = p_train;       
train_t = train_index;       
test_x = p_test;         
test_t = validationSet_index;      
train_t= ind2vec(train_t');
test_t= ind2vec(test_t');
net = lvqnet(20,0.1,'learnlv1'); 
net.trainParam.lr = 0.00001;
net.trainParam.goal = 0.1;
net.trainParam.epochs =40;
net = train(net,train_x,train_t);  
train_y = net(train_x);            
y1 = vec2ind(train_y);              
t1 = vec2ind(train_t);             
acc1 = sum(y1==t1)/numel(t1);      
fprintf('训练集精度%f\n',acc1);
[ko,kp]=find(t1==1);
[ko1,kp1]=find(t1==2);
aa1=sum(ko);
bb1=sum(ko1);
figure;
plotconfusion(categorical(y1),categorical(t1))
test_y = net(test_x);                 
y2 = vec2ind(test_y);                 
t2 = vec2ind(test_t);                
acc2 = sum(y2==t2)/numel(t2);        
fprintf('测试集精度%f\n',acc2);
[ko,kp]=find(t2==1);
[ko1,kp1]=find(t2==2);
aa2=sum(ko);
bb2=sum(ko1);
figure;
plotconfusion(categorical(y2),categorical(t2))
x=[acc1*100,bb1,aa1;acc2*100,bb2,aa2];
figure;
bar(x)
ax = gca;
[mm10,nn10]=find(newtime==2021);
newdata1=newdata(mm10,:);
newlabel1=cell2mat(newlabel(mm10,:));
total_N = size(newdata1,1);     
trainSet_N = round(total_N);   
id = randperm(total_N);
trainSet_id = id(:,1:trainSet_N); 
trainSet = newdata1(trainSet_id,:);
train_index=newlabel1(trainSet_id,:);
[p_train, ps_input] = mapminmax(trainSet', 0, 1);
train_x = p_train;       
train_t = train_index;       
train_t= ind2vec(train_t');
train_y = net(train_x);            
y3 = vec2ind(train_y);              
t3 = vec2ind(train_t);             
acc3 = sum(y3==t3)/numel(t3);      
figure;
plotconfusion(categorical(y3),categorical(t3))

[mm,nn]=size(y3);
aa3=0
bb3=0
for i=1:nn
    if t3(i)==y3(i)
        aa3=aa3+1;
    else
        bb3=bb3+1;
    end
    
end
a=[aa3,bb3,acc3*100];
figure;
plot(a,'color','b','LineWidth',2)
raw112=raw1(1,:);

newdata=raw112(:,hfeatures);
